<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class ShopFrontEndController extends MY_Controller {
	public $shop;

	public function __construct($params) {
        parent::__construct();
        $this->shop = $params['shop'];
		$this->load->library('frontend_lib', array('_ci'=>$this));
		$this->load->model('repo/shop_model', 'shop_model');
		$this->load->model('repo/shop_product_model', 'shop_product_model');
    }

    public function getModule($viewName, $data = null) {
        $data['shop'] = $this->shop;
        $this->load->view('shop-themes/' . $this->shop->template . '/modules/' . $viewName . '.html.php', $data);
    }

    public function instruct($shop_slug) {
        $this->renderView('instruct', array('pageTitle' => 'Hướng dẫn mua hàng'));
    }

    public function intro($shop_slug) {
        $this->renderView('intro', array('pageTitle' => 'Giới thiệu'));
    }

	public function getNewProduct($limit=PRODUCT_LIMIT, $isModule=false) {
		$aProduct = $this->shop_product_model->getNewProducts($this->shop->getId(), $limit);
		$aReturn  = array('aProduct' => $aProduct);
		if($isModule){
			$this->getModule('new', $aReturn);
		} else {
			$this->renderView('modules/new', $aReturn);
		}
	}

	public function getSaleProduct($limit=PRODUCT_LIMIT, $isModule=false) {
		$aProduct = $this->shop_product_model->getSaleProducts($this->shop->getId(), $limit);
		$aReturn  = array('aProduct' => $aProduct);
		if($isModule){
			$this->getModule('sale', $aReturn);
		} else {
			$this->renderView('modules/sale', $aReturn);
		}
	}

	public function getHighlightProduct($limit=PRODUCT_LIMIT, $isModule=false, $aProduct=array()) {
		$aProduct = count($aProduct) > 0 ? $aProduct : ($this->shop_product_model->getHighlightProducts($this->shop->getId(), $limit));
		$aReturn  = array('aProduct' => $aProduct);
		if($isModule){
			$this->getModule('highlight', $aReturn);
		} else {
			$this->renderView('modules/highlight', $aReturn);
		}
	}

	public function getAllProduct() {
		$aOption = array(
			'e.id' => $this->shop->user_id
		);
		$aSelect = array('
			a.id, a.slug, a.sale_flg, a.title,
			b.id as product_detail_id, b.price_new, b.price_old, b.is_avatar, b.image, b.description,
			f.id as shop_cate_id,
			e.image_folder as shop_image_folder'
		);
		$aProduct = $this->shop_product_model->splitProductByCategory($aOption, $aSelect, false);
		$aReturn  = array(
			'pageTitle'=> 'Sản phẩm',
			'aProduct' => $aProduct
			);
		$this->renderView('products', $aReturn);
	}

	public function getProduct($params=array()) {
		$product_slug = $params[1];
		$select = array(
			'a.id, a.title, a.slug, a.shop_cate_id, a.active_flg, a.deleted_at,
			f.title AS shop_cate_title, f.slug AS shop_cate_slug,
			e.image_folder AS shop_image_folder'
			);
		$product = $this->shop_product_model->get($product_slug, array(), $select, false);
		if($product == null) { show_404(); }
		$this->load->model('repo/product_model', 'product_model');
		$productDetails = $this->product_model->getProductDetails($product->getId());
		$aReturn = array(
			'pageTitle'      => 'Sản phẩm',
			'product'        => $product,
			'productDetails' => $productDetails
			);
		$this->renderView('single-product', $aReturn);
	}

	public function getProductByCategory($params=array()) {
		$cate_slug = $params[1];
		$this->load->model('repo/category_model', 'category_model');
		$category = $this->category_model->getShopCategoryBySlug($cate_slug, $this->shop->getId());
		if ($category == null) { show_404(); }
		$aProduct    = $this->shop_product_model->getShopCategoryProducts($this->shop->getId(), $cate_slug);
		$aReturn     = array(
			'pageTitle' => $category->getTitle(),
			'category'  => $category,
			'aProduct'  => $aProduct
			);
		$this->renderView('category', $aReturn);
	}
}
?>